/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include "SmbHandler.h"
#include "ProtectPluginFactory.h"
#include "PluginConstants.h"
#include "PluginUtilities.h"

#include "gtest/gtest.h"
#include "mockcpp/mockcpp.hpp"

using namespace PluginUtils;
namespace {
    constexpr auto MODULE = "SmbHandler";
    constexpr int MP_FAILED  = -1;
    constexpr int MP_SUCCESS  = 0;
}

class SmbHandlerTest : public testing::Test {
public:
    void SetUp();
    void TearDown();
    static void SetUpTestCase();
    static void TearDownTestCase();
    std::unique_ptr<SmbHandler> m_ins;
};

void SmbHandlerTest::SetUp()
{
    m_ins = std::make_unique<SmbHandler>();
}

void SmbHandlerTest::TearDown()
{
    GlobalMockObject::verify(); // 校验mock规范并清除mock规范
}

void SmbHandlerTest::SetUpTestCase()
{}

void SmbHandlerTest::TearDownTestCase()
{}

/*
 * 用例名称：ListNativeResource
 * 前置条件：无
 * check点：列出本地资源
 **/
TEST_F(SmbHandlerTest, ListNativeResource)
{
    MOCKER_CPP(&PluginUtils::KinitTGTInner) // mockcpp 模板函数打桩
            .stubs()
            .will(returnValue(0))
            .then(returnValue(-1));
    FileResourceInfo resourceInfo;
    ListResourceParam listResourceParam;
    listResourceParam.nasShareAuthInfo.authType = KRB5AUTHMODE;
    EXPECT_NO_THROW(m_ins->ListNativeResource(resourceInfo, listResourceParam));
    EXPECT_NO_THROW(m_ins->ListNativeResource(resourceInfo, listResourceParam));
}

/*
 * 用例名称：ReadCifsResourceDir
 * 前置条件：无
 * check点：读cifs资源目录
 **/
TEST_F(SmbHandlerTest, ReadCifsResourceDir)
{
    MOCKER_CPP(smb2_connect_share)
            .stubs()
            .will(returnValue(1));
    FileResourceInfo resourceInfo;
    ListResourceParam listResourceParam;
    std::string version;
    EXPECT_EQ(m_ins->ReadCifsResourceDir(resourceInfo, listResourceParam, version), false);
    listResourceParam.sharePath = "file.txt";
    listResourceParam.nasShareAuthInfo.authType = "5";
    EXPECT_EQ(m_ins->ReadCifsResourceDir(resourceInfo, listResourceParam, version), false);
}

/*
 * 用例名称：InvokeSmbOpenDir
 * 前置条件：无
 * check点：读cifs资源目录
 **/
TEST_F(SmbHandlerTest, InvokeSmbOpenDir)
{
    smb2dir *smbDir = nullptr;
    MOCKER_CPP(smb2_opendir)
            .stubs()
            .will(returnValue(smbDir));
    struct smb2dirent *smbdirent = nullptr;
    MOCKER_CPP(smb2_readdir)
            .stubs()
            .will(returnValue(smbdirent));
    MOCKER_CPP(smb2_destroy_url) 
            .stubs()
            .will(ignoreReturnValue());
    FileResourceInfo resourceInfo;
    struct smb2_context* smbContext = nullptr;
    ListResourceParam listResourceParam;
    listResourceParam.path = "/tmp";
    struct smb2_url *url;
    EXPECT_EQ(m_ins->InvokeSmbOpenDir(resourceInfo, smbContext, listResourceParam, url), true);
}

/*
 * 用例名称：SetKrbEnvironment
 * 前置条件：无
 * check点：读cifs资源目录
 **/
TEST_F(SmbHandlerTest, SetKrbEnvironment)
{
    SmbConnectParams smbConnectParams;
    smbConnectParams.krb5ConfigFile = "a";
    EXPECT_EQ(m_ins->SetKrbEnvironment(smbConnectParams.krb5CcnameFile, smbConnectParams.krb5ConfigFile), -1);
    smbConnectParams.krb5CcnameFile = "a";
    EXPECT_EQ(m_ins->SetKrbEnvironment(smbConnectParams.krb5CcnameFile, smbConnectParams.krb5ConfigFile), -1);

    MOCKER_CPP(stat)
            .stubs()
            .will(returnValue(0));
    EXPECT_EQ(m_ins->SetKrbEnvironment(smbConnectParams.krb5CcnameFile, smbConnectParams.krb5ConfigFile), -1);

    FileResourceInfo resourceInfo;
    ListResourceParam listResourceParam;
    EXPECT_NO_THROW(m_ins->ListAggregateResource(resourceInfo, listResourceParam));  
}

